package com.open.base.aspect;

import com.alibaba.fastjson2.JSON;
import com.open.common.annotation.InterfaceExtend;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @author dashu03
 * @date 2025年07月04日 15:55
 * @description 接口扩展切面
 */
@Aspect
@Component
public class InterfaceExtendAspect {

    private static final Logger log = LoggerFactory.getLogger(InterfaceExtendAspect.class);

    @Around("@annotation(interfaceExtend)")
    public Object extendAround(ProceedingJoinPoint joinPoint, InterfaceExtend interfaceExtend) throws Throwable {
        // 如果未开启日志记录，直接执行原方法
        if (!interfaceExtend.logParams()) {
            return joinPoint.proceed();
        }

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String methodName = signature.getName();
        String title = interfaceExtend.title();
        // 打印入参
        Object[] args = joinPoint.getArgs();
        log.info("{} - {}，请求参数: {}", title, methodName, JSON.toJSONString(args));

        // 执行目标方法
        Object result = joinPoint.proceed();

        // 打印出参
        log.info("{} - {}，响应结果: {}", title, methodName, JSON.toJSONString(result));
        return result;
    }
}
